package com.opensource.component.swagger.config;

import com.opensource.component.swagger.config.SwaggerConfig.DocketInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

/**
 * @description: Swagger Resource 配置
 * @author: wangweir
 * @date: 2019/5/17 15:49
 */
@Component
@Primary
public class ResourceProvider implements SwaggerResourcesProvider {

  @Autowired
  private SwaggerConfig config;

  @Override
  public List<SwaggerResource> get() {
    Map<String, DocketInfo> docketInfoMap = config.getDocket();
    List<SwaggerResource> resources = new ArrayList<>();
    for (Entry<String, DocketInfo> entry : docketInfoMap.entrySet()) {
      resources.add(swaggerResource(entry.getKey(), "/doc/" + entry.getKey()));
    }
    return resources;
  }

  private SwaggerResource swaggerResource(String name, String location) {
    SwaggerResource swaggerResource = new SwaggerResource();
    swaggerResource.setName(name);
    swaggerResource.setLocation(location);
    swaggerResource.setSwaggerVersion("2.0");
    return swaggerResource;
  }


}